package com.red.star.macalline.sched.mapper;

import com.red.star.macalline.data.core.base.MyMapper;
import com.red.star.macalline.sched.entity.SmsLog;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Service;

/**
 * @author nofish.yan@gmail.com
 * @date 2018/3/21.
 * 短信日志数据访问接口
 */
public interface SmsLogMapper extends MyMapper<SmsLog> {
    @Select("SELECT count(s.id) " +
            "FROM t_sms_log AS s " +
            "JOIN t_fans AS f ON s.fans_id = f.id " +
            "WHERE s.fans_id = #{fansId} " +
            "AND (" +
            "(s.chat_session_id = #{chatSessionId} AND s.create_time > DATE_FORMAT(CURRENT_DATE(), '%Y-%m-%d %H:%i:%s')) " +
            "OR (f.type = 0 AND TIME_TO_SEC(TIMEDIFF( now( ), s.create_time )) / 60 < #{cIntervalTime}) " +
            "OR (f.type = 1 AND TIME_TO_SEC(TIMEDIFF( now( ), s.create_time )) / 60 < #{bIntervalTime}))")
    int countSmsLogByFansAndSession(@Param("fansId") Integer fansId, @Param("chatSessionId") String chatSessionId,
                                    @Param("cIntervalTime") Integer cIntervalTime, @Param("bIntervalTime") Integer bIntervalTime);

}
